package com.jshoperxms.websocket;

import com.jshoperxms.controller.utils.DateTools;
import com.jshoperxms.controller.utils.PrimaryKey;
import com.jshoperxms.controller.utils.enums.BaseEnums;
import com.jshoperxms.controller.utils.json.GsonJson;
import com.jshoperxms.controller.utils.statickey.StaticKey;
import com.jshoperxms.entity.YdAncientMatchRdT;
import com.jshoperxms.entity.YdAncientQuestionsT;
import com.jshoperxms.service.YdAncientMatchRdTService;
import com.jshoperxms.service.YdAncientQuestionsTService;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * Created by sdywcd on 2018/12/15.
 * Des: 关于比赛的数据处理
 */
@Component
public class AncientMatchService {

    @Resource
    private YdAncientQuestionsTService ydAncientQuestionsTService;

    @Resource
    private YdAncientMatchRdTService ydAncientMatchRdTService;

    /**
     * 生成随机的10题目
     * @return
     */
    public List<YdAncientQuestionsT> buildMatchAncs(){
        String sql="SELECT * FROM yd_ancient_questions_t WHERE usedamount >= (SELECT floor(RAND() * (SELECT MAX(usedamount) FROM yd_ancient_questions_t))) ORDER BY ancnumber LIMIT 10";
        List<YdAncientQuestionsT>list=this.ydAncientQuestionsTService.queryBySql(YdAncientQuestionsT.class,sql);
        return list;
    }


    /**
     * 创建比赛记录
     * @param rq
     * @return
     */
    public YdAncientMatchRdT createMatchRd(ActionRuleVo rq){
        List<YdAncientQuestionsT>questionsTS=this.buildMatchAncs();
        YdAncientMatchRdT bean=new YdAncientMatchRdT();
        if(StringUtils.isNotBlank(rq.getMatchmodel())){
            if(StringUtils.equals("single",rq.getMatchmodel())){
                bean.setId(PrimaryKey.get().toString());
                bean.setStatus(BaseEnums.DataUsingState.USING.getState());
                bean.setCreatetime(DateTools.getSystemTimeLong());
                bean.setCreatorid("0");
                bean.setUpdatetime(DateTools.getSystemTimeLong());
                bean.setVersiont(1);
                bean.setHomewxopenid(rq.getWxopenid());
                bean.setAwaywxopenid(StaticKey.EMPTY);
                bean.setHomenick(rq.getHomenick());
                bean.setAwaynick(StaticKey.EMPTY);
                bean.setMatchtime(DateTools.getSystemTimeLong());
                bean.setMatchtitle(bean.getHomenick()+"single比赛");
                bean.setMatchitemscontent(GsonJson.parseDataToJson(questionsTS));
                bean.setHomescore(0);
                bean.setAwayscore(0);
                bean.setHomerightcount(0);
                bean.setHomewrongcount(0);
                bean.setAwayrightcount(0);
                bean.setAwaywrongcount(0);
                bean.setWhowin(0);
                bean.setMatchstatus("1");//进行中
                bean.setMatchmodel(rq.getMatchmodel());
                bean.setMatchid(rq.getMatchid());
                bean.setHomematchusedtime(0);
                bean.setAwaymatchusedtime(0);
                this.ydAncientMatchRdTService.save(bean);
            }
            if(StringUtils.equals("battle",rq.getMatchmodel())) {
                bean.setId(PrimaryKey.get().toString());
                bean.setStatus(BaseEnums.DataUsingState.USING.getState());
                bean.setCreatetime(DateTools.getSystemTimeLong());
                bean.setCreatorid("0");
                bean.setUpdatetime(DateTools.getSystemTimeLong());
                bean.setVersiont(1);
                bean.setHomewxopenid(rq.getWxopenid());
                bean.setAwaywxopenid(StaticKey.EMPTY);
                bean.setHomenick(rq.getHomenick());
                bean.setAwaynick(StaticKey.EMPTY);
                bean.setMatchtime(DateTools.getSystemTimeLong());
                bean.setMatchtitle(bean.getHomenick()+"battle比赛");
                bean.setMatchitemscontent(GsonJson.parseDataToJson(questionsTS));
                bean.setHomescore(0);
                bean.setAwayscore(0);
                bean.setHomerightcount(0);
                bean.setHomewrongcount(0);
                bean.setAwayrightcount(0);
                bean.setAwaywrongcount(0);
                bean.setWhowin(0);
                bean.setMatchstatus("1");//进行中
                bean.setMatchmodel(rq.getMatchmodel());
                bean.setMatchid(rq.getMatchid());
                bean.setHomematchusedtime(0);
                bean.setAwaymatchusedtime(0);
                this.ydAncientMatchRdTService.save(bean);
            }
        }
        return bean;
    }

    /**
     * 更新单人比赛数据
     * @param rq
     * @return
     */
    public YdAncientMatchRdT updateSingleMatchInfo(ActionRuleVo rq){
        Criterion criterion=Restrictions.and(Restrictions.eq("homewxopenid",rq.getWxopenid())).add(Restrictions.eq("matchid",rq.getMatchid()));
        YdAncientMatchRdT bean=this.ydAncientMatchRdTService.findOneByCriteria(YdAncientMatchRdT.class,criterion);
        if(bean!=null){
            bean.setHomescore(rq.getHomescore());
            bean.setHomerightcount(rq.getHomerightcount());
            bean.setHomewrongcount(rq.getHomewrongcount());
            bean.setMatchstatus("2");//比赛结束
            bean.setHomematchusedtime(rq.getHomematchusedtime());
            bean.setUpdatetime(DateTools.getSystemTimeLong());
            bean.setVersiont(bean.getVersiont()+1);
            this.ydAncientMatchRdTService.update(bean);
            return bean;
        }
        return null;
    }



}
